// subcontract/Mine/merchantsSettle/merchants.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    aliyunServrBaseURL: getApp().config.aliyunServrBaseURL,//阿里云公共地址
    s: app.system.statusBarHeight, //状态栏高度
    n: (app.menu.top - app.system.statusBarHeight) * 2 + app.menu.height, //导航栏高度
    h: app.menu.height, //胶囊高度
    aliyunServrBaseURL: getApp().config.aliyunServrBaseURL,//阿里云公共地址
    baseURL: getApp().config.baseURL,//公共地址
    tips: '获取验证码',
    countdown: 60, // 倒计时初始值
    disabled: false, // 按钮是否禁用
    name: '',//商家
    nickname: '',//姓名
    phoneNumber: '',//手机号
    code: '',//验证码
    type: '1',//返回页面状态
    columns: [],//类目数组
    pickerShow: false,//类目状态
    categoryInfo: {},//选择的类目
    iseye: false,//眼睛是否显示密码
    license: '',//营业执照
    activeIndex: 0,
    info: {},//配置信息
    count: [],
    isSelected: false,//是否选中
    showLoginPop: false,//第一次登录  填写用户资料
  },
  /**配置 */
  async getuzhuTemplate() {
    const ret = await getApp().api.getuzhuTemplate()
    const res = await getApp().api.getAdTemplate()
    if (ret.code == 1 && res.code == 1) {
      let obj = {
        list: res.data
      }
      this.setData({
        info: { ...obj, ...ret.data }
      })
    }
  },
  tabs(event) {
    console.log(event);
    let index = event.currentTarget.dataset.index
    this.setData({
      activeIndex: index
    })
  },
  /**选取图片 */
  async chooseImage() {
    const { errMsg, tempFiles } = await wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      camera: 'back',
    })
    if (errMsg == 'chooseMedia:ok') {
      this.uploadFilePromise(tempFiles[0].tempFilePath)
    }
  },
  /**上传营业执照 */
  async uploadFilePromise(url) {
    const { code, data } = await getApp().api.uploadFilePromise(url)
    if (code == 1) {
      this.setData({
        license: data.uri
      })
    }
  },
  /**切换显示密码 */
  switchIseye() {
    this.setData({
      iseye: !this.data.iseye
    })
  },
  /**获取手机号 */
  async getPhoneNumber(e) {
    console.log(e);
    if (e.detail.code) {
      let objrct = {
        code: e.detail.code,
      }
      const { code, data } = await getApp().api.getPhoneNumber(objrct)
      if (code == 1) {
        this.setData({
          phoneNumber: data.phone_info.phoneNumber
        })
      }
    }
  },
  /**申请记录 */
  async record() {
    const { code, data } = await getApp().api.record()
    if (code == 1) {
      console.log(data.lists);
      if (data.count != 0) {
        this.setData({
          count: data.count,
          name: data.lists[0].name,
          nickname: data.lists[0].nickname,
          phoneNumber: data.lists[0].mobile,
          password: data.lists[0].password,
          license: data.lists[0].license[0],
        })
      } else {
        this.setData({
          count: data.count,
        })
      }

    }
  },
  /**商家入职提交 */
  async Merchant() {
    if (!this.data.name) return getApp().showToast('请输入公司或企业名称')
    if (!this.data.nickname) return getApp().showToast('请输入姓名')
    if (!getApp().validatePhoneNumber(this.data.phoneNumber)) return getApp().showToast('请输获取手机号')
    if (!this.data.password) return getApp().showToast('请输入密码')
    if (!this.data.categoryInfo.id) return getApp().showToast('请输选择直营类目')
    if (!this.data.license) return getApp().showToast('请上传营业执照')
    let object = {
      cid: this.data.categoryInfo.id,//类目id
      name: this.data.name,//企业名字
      nickname: this.data.nickname,//姓名
      mobile: this.data.phoneNumber,//手机号  账号
      account: this.data.phoneNumber,//手机号  账号
      password: this.data.password,//密码
      license: [this.data.license],//营业执照
    }
    console.log(object);
    const { code, data } = await getApp().api.ShopApply(object)
    if (code == 1) {
      this.ruzhucharge()
    }
  },
  confirmsss() {
    this.setData({
      isSelected: !this.data.isSelected
    })
    this.ruzhucharge()
  },
  /** */
  async ruzhucharge() {
    if (!this.data.isSelected) return this.setData({
      showModel: true
    })
    let object = {
      feetype: this.data.activeIndex,
    }
    const { code, data } = await getApp().api.ruzhucharge(object)
    if (code == 1) {
      this.unifiedpay(data.order_sn)
    }
  },
  /**调用支付 */
  async unifiedpay(order_id) {
    let object = {
      from: 'ruzhucharge',
      order_id,
      pay_way: 1
    }
    const { code, data } = await getApp().api.unifiedpay(object)
    if (code == 1) {
      const ret = await getApp().requestPay(data)
      if (ret.errMsg == 'requestPayment:ok') {
        // navigateTo('/pages/Home/resultPay/resultPay?type=pay')
        console.log('支付成功');
        getApp().showToast('支付成功!')
        wx.redirectTo({
          url: '/subcontract/Mine/successfullySettled/index'
        })
      } else {
        getApp().showToast('支付失败！')
        this.record()
      }
    }
  },
  /**打开类目选择 */
  openCategory() {
    this.setData({
      pickerShow: true
    })
  },
  /**获取主营类目列表 */
  async getcategoryList() {
    const { code, data } = await getApp().api.getcategoryList()
    if (code == 1) {
      this.setData({
        columns: data
      })
    }
  },
  /**确定选择了类目 */
  confirm(event) {
    console.log(event);
    this.setData({
      pickerShow: false,
      categoryInfo: event.detail.value
    })
  },
  /**关闭类目选择 */
  onClose() {
    this.setData({
      pickerShow: false
    })
    this.record()
  },
  inputHd(event) {
    console.log(event);
    let { value } = event.detail
    let { type } = event.currentTarget.dataset
    this.setData(
      type == 'corporateName' ? { name: value } : type == 'name' ? { nickname: value }:type == 'password' ? { password: value }:''
    )
    // let type = event.target.dataset.type
    // console.log(type);
    // this.setData(type == 'corporateName' ? { corporateName: event.detail.value } : type == 'name' ? { name: event.detail.value } : type == 'password' ? { password: event.detail.value } : '')
  },
  /**跳过 */
  redirectTo() {
    console.log(1);
    if (this.data.type == 1) {
      wx.switchTab({
        url: '/pages/Home/home/home'
      })
    } else {
      wx.navigateBack()
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getuzhuTemplate()
    this.record()
    this.setData({
      type: options.type || ''
    })
    this.getcategoryList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})